package com.turing.system.service.impl;

import java.math.BigDecimal;

import cn.hutool.core.date.DateUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.turing.system.entity.KeeperWithdrawals;
import com.turing.system.service.KeeperWithdrawalsService;
import com.turing.system.mapper.KeeperWithdrawalsMapper;
import enums.ErrorCode;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import result.Result;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author admin
 * @description 针对表【keeper_withdrawals(店主或管理员流水)】的数据库操作Service实现
 * @createDate 2023-05-08 16:40:08
 */
@Service
public class KeeperWithdrawalsServiceImpl extends ServiceImpl<KeeperWithdrawalsMapper, KeeperWithdrawals>
        implements KeeperWithdrawalsService {


    /**
     * 增加商家流水
     *
     * @param json
     * @return
     */
    @Override
    public Result kwAddSave(JSONObject json) {
        String sourceId = json.getString("sourceId");
        String targetId = json.getString("targetId");
        Double kwValue = json.getDouble("kwValue");
        Integer kwType = json.getInteger("kwType");
        String kwInfo = json.getString("kwInfo");
        Double sourceReValue = json.getDouble("sourceReValue");
        Double targetReValue = json.getDouble("targetReValue");
        String kwCreateTime = json.getString("kwCreateTime");

        KeeperWithdrawals withdrawals = new KeeperWithdrawals();
        withdrawals.setKwId(UUID.randomUUID().toString());
        if (sourceId != null) {
            withdrawals.setSourceId(sourceId);
        } else {
            withdrawals.setSourceId(null);
        }
        if (targetId != null) {
            withdrawals.setTargetId(targetId);
        } else {
            withdrawals.setTargetId(null);
        }
        if (kwValue != null){
            withdrawals.setKwValue(new BigDecimal(kwValue));
        }else {
            withdrawals.setKwValue(null);
        }

        withdrawals.setKwType(kwType);
        withdrawals.setKwInfo(kwInfo);
        if (sourceReValue != null) {
            withdrawals.setSourceReValue(new BigDecimal(sourceReValue));

        } else {
            withdrawals.setSourceReValue(null);

        }

        if (targetReValue != null) {
            withdrawals.setTargetReValue(new BigDecimal(targetReValue));

        } else {
            withdrawals.setTargetReValue(null);

        }
        withdrawals.setKwCreateTime(DateUtil.parse(kwCreateTime));

        int insert = this.baseMapper.insert(withdrawals);
        System.out.println("withdrawals = " + withdrawals);
        System.out.println("insert = " + insert);
        if (insert > 0) {
            return Result.success();
        } else {
            return Result.fail(ErrorCode.ERROR_ADD);
        }
    }

    @Resource
    private RedisTemplate redisTemplate;

    private final String SESSIONS_CACHE_PREFIX = "keeper:list:";

    private final String SESSIONS_CACHE_NAME = "name:List:";


    @Override
    public List<LinkedHashMap<String, Object>> kwList(String merchantName,Integer pageNum,Integer pageSize) {
        ListOperations ops = redisTemplate.opsForList();
        if (StringUtils.isEmpty(merchantName)) {

            List<LinkedHashMap<String, Object>> linkedHashMaps = this.baseMapper.selectListEntity();
            linkedHashMaps.forEach(map -> {
                double kwValue;
                String merchantId;
                String createTime = map.get("kw_create_time").toString().replace("T", " ");
                if (map.get("kw_value") == null) {
                    kwValue = 0.00;
                } else {
                    kwValue = Double.parseDouble(map.get("kw_value").toString());
                }
                String kwInfo = map.get("kw_info").toString();
                if (map.get("merchant_id") == null) {
                    merchantId = "111";
                } else {
                    merchantId = map.get("merchant_id").toString();
                }
                if (kwInfo != null && kwInfo.contains("客官付收款")) {
                    map.remove("kw_info");
                    map.put("kwInfo", "客官付收款");
                } else if (kwInfo != null && kwInfo.contains("商家充值")) {
                    map.remove("kw_info");
                    map.put("kwInfo", "商家充值");
                } else if (kwInfo != null && kwInfo.contains("销售商品收入")) {
                    map.remove("kw_info");
                    map.put("kwInfo", "销售商品收入");
                } else if (kwInfo != null && kwInfo.contains("商家提现")) {
                    map.remove("kw_info");
                    map.put("kwInfo", "商家提现");
                } else if (kwInfo != null) {
                    map.remove("kw_info");
                    map.put("kwInfo", kwInfo);
                }
                //•	出项：商家提现%，发布悬赏任务，普通评论支出
                if ("发布悬赏任务".equals(kwInfo) || "普通评论支出".equals(kwInfo) || Objects.requireNonNull(kwInfo).contains("商家提现")) {
                    map.remove("kw_value");
                    map.put("kwValue", Double.parseDouble("-" + kwValue));
                    Double sum = this.baseMapper.getSum(kwInfo, merchantId);
                    map.remove("kw_id");
                    map.put("totalValue", sum == null ? 0.00 : Double.parseDouble("-" + sum));
                } else {
                    map.remove("kw_value");
                    map.put("kwValue", kwValue);
                    Double sum = this.baseMapper.getSum(kwInfo, merchantId);
                    map.remove("kw_id");
                    map.put("totalValue", sum == null ? 0.00 : sum);
                }
                map.remove("merchant_id");
                map.remove("kw_create_time");
                map.put("kwCreateTime", createTime);
            });
            ops.rightPushAll(SESSIONS_CACHE_PREFIX,linkedHashMaps);
            return linkedHashMaps;
        }
        List<LinkedHashMap<String, Object>> listMap = this.baseMapper.selectListMap(merchantName);
        listMap.forEach(map -> {
            double kwValue;
            String createTime = map.get("kw_create_time").toString().replace("T", " ");
            if (map.get("kw_value") == null) {
                kwValue = 0.00;
            } else {
                kwValue = Double.parseDouble(map.get("kw_value").toString());
            }
            String kwInfo = map.get("kw_info").toString();
            String merchantId = map.get("merchant_id").toString();
            if (kwInfo != null && kwInfo.contains("客官付收款")) {
                map.remove("kw_info");
                map.put("kwInfo", "客官付收款");
            } else if (kwInfo != null && kwInfo.contains("商家充值")) {
                map.remove("kw_info");
                map.put("kwInfo", "商家充值");
            } else if (kwInfo != null && kwInfo.contains("销售商品收入")) {
                map.remove("kw_info");
                map.put("kwInfo", "销售商品收入");
            } else if (kwInfo != null && kwInfo.contains("商家提现")) {
                map.remove("kw_info");
                map.put("kwInfo", "商家提现");
            } else if (kwInfo != null) {
                map.remove("kw_info");
                map.put("kwInfo", kwInfo);
            }
            //•	出项：商家提现%，发布悬赏任务，普通评论支出
            if ("发布悬赏任务".equals(kwInfo) || "普通评论支出".equals(kwInfo) || Objects.requireNonNull(kwInfo).contains("商家提现")) {
                map.remove("kw_value");
                map.put("kwValue", Double.parseDouble("-" + kwValue));
                Double sum = this.baseMapper.getSum(kwInfo, merchantId);
                map.remove("kw_id");
                map.put("totalValue", sum == null ? 0.00 : Double.parseDouble("-" + sum));
            } else {
                map.remove("kw_value");
                map.put("kwValue", kwValue);
                Double sum = this.baseMapper.getSum(kwInfo, merchantId);
                map.remove("kw_id");
                map.put("totalValue", sum == null ? 0.00 : sum);
            }
            map.remove("merchant_id");
            map.remove("kw_create_time");
            map.put("kwCreateTime", createTime);
        });

        return listMap;
    }
    @Override
    public List<LinkedHashMap<String, Object>> kwListMap() {
        return this.baseMapper.selectListEntity();
    }


}




